<?php

namespace Admin\Controller;

class MainController extends CommonController
{
    // 后台主页
    public function index()
    {
        $this->display();
    }

    // 顶部菜单
    public function top_menu()
    {
        $menu = $this->get_admin_menu(0);
        $user = D('User')->get_user_one(session('ytata.uid'), true, 1);

        $this->assign('menu', $menu);
        $this->assign('user', $user);

        $this->display();
    }

    // 左侧菜单
    public function left_menu()
    {
        $menu_id = I('get.menu_id', '1', 'intval');

        $menu = $this->get_admin_menu($menu_id);

        $this->assign('menu', $menu);

        $this->display();
    }

    // 管理中心
    public function main()
    {
        $message = array();
        if (APP_DEBUG == true) {
            $message[] = array(
                'type'    => 'error',
                'content' => "系统debug处于打开模式，建议关闭，关闭后可提升系统运行效率。",
            );
        }
        // 检测是否有新版本
        // $ret = $this->check_newverson();
        // if ($ret[0]) {
        //     list($newver, $curver) = $ret[1];
        //     $message[] = array(
        //         'type'    => 'error',
        //         'content' => "系统有新版本[{$newver}]，您当前版本为[{$curver}]。",
        //     );
        // }

        $wait_info      = $this->wait_info();  //待办
        $today_info     = $this->today_info();  // 今日统计
        $yesterday_info = $this->yesterday_info();  // 昨日统计
        $system_info    = $this->system_info();  // 系统信息
        $count_info     = $this->count_info();  // 总数统计

        $this->assign(compact('message', 'wait_info', 'today_info', 'yesterday_info', 'count_info', 'system_info'));

        $this->display();
    }

    /**
     * 获取后台菜单
     *
     * @param int $pid 父菜单ID
     * @return array
     */
    private function get_admin_menu($pid = 0)
    {
        $menu = ytata_get_cache('admin_menu');

        $arr = array();
        foreach ($menu as $key => $v) {
            if ($v['m_pid'] == $pid) {
                $arr[] = $v;
            }
        }

        return $arr;
    }

    /**
     * 代办事项
     *
     * @return array
     */
    private function wait_info()
    {
        $map['audit'] = 0;

        $wait_info['question'] = M('Question')->where($map)->count();
        $wait_info['article']  = M('Article')->where($map)->count();
        $wait_info['tag']      = M('Tag')->where($map)->count();

        return $wait_info;
    }

    /**
     * 今日统计
     *
     * @return array
     */
    private function today_info()
    {
        $today = get_mktime(0);

        $map['addtime']      = array(array('gt', $today[0]), array('lt', $today[1]));
        $usermap['reg_time'] = array(array('gt', $today[0]), array('lt', $today[1]));

        $today_info['user']     = M('User')->where($usermap)->count();
        $today_info['question'] = M('Question')->where($map)->count();
        $today_info['article']  = M('Article')->where($map)->count();
        $today_info['tag']      = M('Tag')->where($map)->count();

        return $today_info;
    }

    /**
     * 昨日统计
     *
     * @return array
     */
    private function yesterday_info()
    {
        $yesterday = get_mktime('-1');

        $map['addtime']      = array(array('gt', $yesterday[0]), array('lt', $yesterday[1]));
        $usermap['reg_time'] = array(array('gt', $yesterday[0]), array('lt', $yesterday[1]));

        $yesterday_info['user']     = M('User')->where($usermap)->count();
        $yesterday_info['question'] = M('Question')->where($map)->count();
        $yesterday_info['article']  = M('Article')->where($map)->count();
        $yesterday_info['tag']      = M('Tag')->where($map)->count();

        return $yesterday_info;
    }

    /**
     * 系统信息
     *
     * @return array
     */
    private function system_info()
    {
        $mysql_version = M()->query("select VERSION()");
        $mysql_version = $mysql_version[0]['version()'];
        $system_info   = array(
            'server_os'     => PHP_OS,
            'web_server'    => $_SERVER["SERVER_SOFTWARE"],
            'php_version'   => PHP_VERSION,
            'mysql_version' => $mysql_version
        );
        return $system_info;
    }

    /**
     * 总数统计
     *
     * @return array
     */
    private function count_info()
    {
        $count_info = array();
        $count_info['user']     = M('User')->count();
        $count_info['question'] = M('Question')->count();
        $count_info['article']  = M('Article')->count();
        $count_info['answer']   = M('Answer')->count();
        $count_info['tag']      = M('Tag')->count();
        $count_info['img']      = M('QuestionImg')->count();
        $count_info['notice']   = M('Notice')->count();

        return $count_info;
    }

    /**
     * @param $message
     * @return array
     */
    //protected function check_newverson()
    //{
    //    $newver = https_request('http://www.yangtata.com/version/getnew.html');
    //    $curver = C('YTATA_VERSION');
    //    if ($curver < $newver) {
    //        return array(true, array($newver, $curver));
    //    } else {
    //        return array(false, array());
    //   }
    //}
}